#include "Server.h"
#include <HLString.h>
#include <LuaManager.h>
#include <LogManager.h>
#include <Windows.h>

IEvent* g_pEvent = 0;

Server::Server()
{
}

Server::~Server()
{
}

void Server::init(IEvent *pEvent)
{
	g_pEvent = pEvent;

	LuaManager::GetInstance().GetLua().new_usertype<Server>("Server",
		sol::constructors<Server()>(),
		"onConnected", &Server::onConnected,
		"onDisconnected", &Server::onDisconnected,
		"onChat", &Server::onChat,
		"onEnter", &Server::onEnter,
		"onLeave", &Server::onLeave);
}

void Server::onConnected(const char* msg)
{
	if (g_pEvent) g_pEvent->onConnected(HL::String::UTF8ToGBK(msg).c_str());
}

void Server::onDisconnected(const char* msg)
{
	if (g_pEvent) g_pEvent->onDisconnected(HL::String::UTF8ToGBK(msg).c_str());
}

void Server::onChat(const char* user, const char* msg)
{
	if (g_pEvent) g_pEvent->onChat(HL::String::UTF8ToGBK(user).c_str(), HL::String::UTF8ToGBK(msg).c_str());
}

void Server::onEnter(const char* msg)
{
	if (g_pEvent) g_pEvent->onEnter(HL::String::UTF8ToGBK(msg).c_str());
}

void Server::onLeave(const char* msg)
{
	if (g_pEvent) g_pEvent->onLeave(HL::String::UTF8ToGBK(msg).c_str());
}
